<template>
    <div class="observer-lbrary-pages">
        <compSearchCriteria
         :formItem="formItem" 
         @searchHandleChange="searchHandleChange" 
         v-model="dataTableConfig.requestParams"
         ref="refSearchCriteria"
         />
         <div class="btn-box" style="margin: 20px 0;">
            <div></div>
            <a-form layout="inline" style="display: flex;justify-content: flex-end;">
                <a-form-item>
                    <a-form
                    layout="inline" 
                    :form="form" >
                        <a-form-item class="fyear-box">
                            <a-input-number
                            placeholder="请输入年度"
                            v-model:value="fyear"
                            :precision="0"
                            v-decorator="[
                                'fyear', { initialValue:fyear, rules: rules.fyear}
                            ]"/>
                        </a-form-item>
                    </a-form>
                </a-form-item>
                <a-form-item>
                    <a-button class="blue-btn" type="primary" @click="initiationYear">发起年度考评</a-button>
                </a-form-item>
                <a-form-item>
                    <a-button class="blue-btn" type="primary" @click="increaseClick" icon>新增</a-button>
                </a-form-item>
                <a-form-item>
                    <a-upload style="display: inline-block;" :beforeUpload="beforeUpload" action="" @change="leadingInClick">
                        <a-button class="blue-btn" type="default"  icon="import">导入</a-button>
                    </a-upload>
                    <!-- <a-button type="primary" @click="leadingInClick">导入</a-button> -->
                </a-form-item>
                <a-form-item>
                    <a-button class="blue-btn" type="primary" icon="download" :loading="leadingOutLoading" @click="leadingOutClick">导出</a-button>
                </a-form-item>
                <a-form-item>
                    <a-button class="blue-btn" type="primary" icon="download" :loading="DownloadTemplateLoading" @click="DownloadTemplateClick">下载模板</a-button>
                </a-form-item>
                <a-form-item>
                    <a-button class="red-btn" type="primary" @click="batchDeletion" >批量删除</a-button>
                </a-form-item>
        </a-form>
         </div>
         
        <div>
            <compTable
             :key="compSelectorkey" 
             :config='dataTableConfig' 
             :columns="columns" 
             :requestParamObj="requestParams" 
             @selectedRowKeys="selectedRowKeys"
             @sorterChange="sorterChange"
             >
                <!-- <template v-slot:url="scope">
                    <span>{{ scope }}</span>
                </template> -->
                <template v-slot:operation="{record}" data-desc="自定操作栏">
                    <a-form layout="inline" class="search-form">
                        <a-form-item >
                            <a-button class="blue-btn" type="primary" @click="reviseClick(record)">编辑</a-button>
                        </a-form-item>
                        <a-form-item >
                            <a-button class="red-btn" type="primary" @click="deleteClick(record)" >删除</a-button>
                        </a-form-item>
                    </a-form>
                </template>
            </compTable>
        </div>
        <!-- <a-modal
         :visible="addEditVisible" 
         :title="addEditParameter.id?'编辑':'新增'" 
         @ok="handleOk" 
         :confirmLoading="confirmLoading" 
         @cancel="visible = false"> -->

         <increase ref="refModel" :addEditParameter="addEditParameter" @handleOk="handleOk"/>
         <increaseAdd ref="refModelAdd" :addEditParameter="addEditParameter" @handleOk="handleOk"/>
            
        <!-- </a-modal> -->
        
    </div>
</template>
<script>
import compSearchCriteria from '@/components/compSearchCriteria/compSearchCriteria'
import compTable from '@/components/compTable/compTable'
import {typeInput, typeNumber, typeSelect, typeCheckbox, typeRadio} from '@/components/compSearchCriteria/ContractType'
import assessCheckApi from './service/api'
import increase from './increase.vue'
import increaseAdd from './increase-add.vue'
import { deleteArrServe, deleteItemServe, leadingInExccel, leadingOutExccel, leadingOutFormwork, batchInitHcyNdkp } from './service/index'
import blobType from '@/common/blobType'
import moment from 'moment'
// import { CodeSandboxCircleFill } from '@yuncheng/ych-components'
  export default {
    components:{
        compSearchCriteria,
        compTable,
        increase,
        increaseAdd
    },
    
    data(){
        return{
            fyear:moment(new Date()).format('YYYY'),
            form: this.$form.createForm(this, { name: 'formNmae' }),
            //end
            addEditParameter:{},
            addEditVisible:false,
            selectedRowKeysArr:[],//选中表格数据
            compSelectorkey:'',
            requestParams:{},
            formItem:[
                {
                    label: '真实姓名',
                    model: 'realname',
                },{
                    label: '工作单位',
                    model: 'fgzdw',
                },{
                    label: '擅长领域',
                    model: 'fszly',
                },{
                    label: '所属区域',
                    model: 'fssqy',
                    // placeholder: '所在地市',
                    // type:typeSelect,
                    // dictionaryType:'观察员所属地市'
                },{
                    label: '所属库',
                    model: 'fssk',
                    placeholder: '所属库',
                    type:typeSelect,
                    dictionaryType:'核查员所属库'
                },{
                    label: '有效状态',
                    model: 'fstate',
                    placeholder: '核查员有效状态',
                    type:typeSelect,
                    dictionaryType:'核查员有效状态'
                }

            ],
            columns: [
                {
                    title: '账号', //列头显示文字
                    dataIndex: 'username',
                    key: 'username',
                    fixed: 'left',
                    width:120,

                },{
                    title: '真实姓名', //列头显示文字
                    dataIndex: 'realname',
                    key: 'realname',
                    fixed: 'left',
                    width:120,
                },{
                    title: '手机号码', //列头显示文字
                    dataIndex: 'phone',
                    key: 'phone',
                },{
                    title: '工作单位',
                    dataIndex: 'fgzdw',
                    key: 'fgzdw',
                    ellipsis: true,//表格省略
                },{
                    title: '工作状态',
                    dataIndex: 'fgzzt',
                    key: 'fgzzt'
                },{
                    title: '有效状态',
                    dataIndex: 'fstate',
                    key: 'fstate'
                },{
                    title: '所属区域',
                    dataIndex: 'fssqy',
                    key: 'fssqy'
                },{
                    title: '所属库',
                    dataIndex: 'fssk',
                    key: 'fssk'
                },{
                    title: '擅长领域',
                    dataIndex: 'fszly',
                    key: 'fszly',
                    ellipsis: true,//表格省略
                },{
                    title: '人员分类',
                    dataIndex: 'fusertype',
                    key: 'fusertype',
                    ellipsis: true,//表格省略
                },{
                    title: '操作',
                    dataIndex: 'id',
                    scopedSlots: {customRender: 'operation'}, // 自定插槽格式
                    fixed: 'right',
                    width:200,
                }
            ],
            dataTableConfig:{
                api: assessCheckApi.list,
                rowKey: 'id',
                rowName: 'fgcyxm',
                rowSelection: {
                    // selectedRowKeys: null,
                    // type: EselectionType.checkbox
                },
                fywflcode:blobType.fywflcodeArr[blobType.menuType.indexOf(this.$route.name.substring(this.$route.name.indexOf('__'),this.$route.name.indexOf('_a')+2))],
            },
            leadingOutLoading:false,
            DownloadTemplateLoading:false,
            rules:{
                fyear: [{
                    type:'number',
                    required: true,
                    message: '请输入年度',
                    trigger: 'blur',
                }],
            },
        }
    },
    mounted() {
        //console.log('监听路由',this.$route)

    },
    
    methods:{
        sorterChange(val,sorter){//获取到排序的数据
            //console.log('获取到排序的数据',val,sorter)
        },
        renovateSearchTabel(){//刷新搜索、表格
            this.compSelectorkey = Date.now()
            this.$refs.refSearchCriteria.resetEvents()
        },
        searchHandleChange(val){//获取搜索数据
            if(val.resetEvents){
                this.compSelectorkey = Date.now()
            }
            this.dataTableConfig.requestParams={...val}
            this.requestParams = {...this.requestParams,...val}
        },
        selectedRowKeys(keys){//获取选中表格rowKey
            //console.log(keys)
            this.selectedRowKeysArr = keys
        },
        increaseClick(){//新增
            this.$refs.refModelAdd.visibleShow = true
            this.addEditParameter = {
                isEdit:false,
                fywflcode:blobType.fywflcodeArr[blobType.menuType.indexOf(this.$route.name.substring(this.$route.name.indexOf('__'),this.$route.name.indexOf('_a')+2))],
                fuserid:'',
                username:'',
                id:'',
                realname:'',
                fsfzh:'',
                sex:'1',
                phone:'',
                email:'',
                fssqy:'南宁',
                fmz:'',
                fzzmm:'',
                fxl:'',
                fbyyx:'',
                fsxzy:'',
                fcsgz:'',
                fzc:'',
                fzw:'',
                fgzdw:'',
                fgzdwdz:'',
                fdwdh:'',
                fdwyx:'',
                fspsj:'',
                fgzzt:'在岗',
                fszly:'',
                flevel:'',
                frkpc:'',
                fssk:'组员库',
                fstate:'有效',
                fusertype:'审评员',
                fzsbh:'',
                fzylx1:'',
                fzylx2:'',
                fzylx3:'',
                fsfyjsry:'是',
                fpywjh:'',
                fyhkh:'',
                fkhhszd:'',
                fkfh:'',
                fxllx:'',
                flx:'',
                fjzqk:'',
                fjcyzg:'',
                fssjcjg:'',
            }
        },
        beforeUpload(e){
            return false
        },
        leadingInClick({file}){//导入
            var that = this
            if(typeof file.uid !== 'undefined') {
                if(file.name.indexOf(".xls") !== -1 || file.name.indexOf(".xlsx") !==-1) {
                    const fileData = new FormData();
                    fileData.append("file",file);
                    fileData.append("fywflcode",blobType.fywflcodeArr[blobType.menuType.indexOf(this.$route.name.substring(this.$route.name.indexOf('__'),this.$route.name.indexOf('_a')+2))]);
                    leadingInExccel(fileData).then(res => {
                        that.renovateSearchTabel()
                        that.$message.success('导入成功')
                    }).catch(error =>{
                        that.$message.success('导入失败')
                    })
                }
                
            }
        },
        leadingOutClick(){//导出
            var that = this
            this.leadingOutLoading = true
            var fileName = '核查员文件'
            let param = { }
            if(that.selectedRowKeysArr.length){
                param = {
                    ids:that.selectedRowKeysArr.join(','),
                    fywflcode:blobType.fywflcodeArr[blobType.menuType.indexOf(this.$route.name.substring(this.$route.name.indexOf('__'),this.$route.name.indexOf('_a')+2))]
                }
            }else{
                param = {...that.requestParams,fywflcode:blobType.fywflcodeArr[blobType.menuType.indexOf(this.$route.name.substring(this.$route.name.indexOf('__'),this.$route.name.indexOf('_a')+2))]}
            }
            
            leadingOutExccel(param).then((data) => {
                this.leadingOutLoading = false
                if (!data) {
                    that.$message.warning('文件下载失败')
                    return
                }
                if (typeof window.navigator.msSaveBlob !== 'undefined') {
                    window.navigator.msSaveBlob(new Blob([data], { type: 'application/vnd.ms-excel' }), fileName + '.xls')
                } else {
                    let url = window.URL.createObjectURL(new Blob([data], { type: 'application/vnd.ms-excel' }))
                    let link = document.createElement('a')
                    link.style.display = 'none'
                    link.href = url
                    link.setAttribute('download', fileName + '.xls')
                    document.body.appendChild(link)
                    link.click()
                    document.body.removeChild(link) //下载完成移除元素
                    window.URL.revokeObjectURL(url) //释放掉blob对象
                }
            })

        },
        DownloadTemplateClick(){//下载模板
            var that = this
            this.DownloadTemplateLoading = true
            var fileName = '核查员模板'
            let param = { fywflcode:blobType.fywflcodeArr[blobType.menuType.indexOf(this.$route.name.substring(this.$route.name.indexOf('__'),this.$route.name.indexOf('_a')+2))]}
            
            leadingOutFormwork(param).then((data) => {
                this.DownloadTemplateLoading = false
                if (!data) {
                    that.$message.warning('文件下载失败')
                return
                }
                // if (typeof window.navigator.msSaveBlob !== 'undefined') {
                //     window.navigator.msSaveBlob(new Blob([data], { type: 'application/vnd.ms-excel' }))
                // } else {
                    let url = window.URL.createObjectURL(new Blob([data], { type: 'application/vnd.ms-excel' }))
                    let link = document.createElement('a')
                    link.style.display = 'none'
                    link.href = url
                    link.setAttribute('download', fileName + '.xls')
                    document.body.appendChild(link)
                    link.click()
                    document.body.removeChild(link) //下载完成移除元素
                    window.URL.revokeObjectURL(url) //释放掉blob对象
                // }
            })
        },
        batchDeletion(){//
            var that = this
            this.$confirm({
                title:'您确认要删除吗？',
                // icon: createVNode(ExclamationCircleOutlined),
                onOk() {
                    if(that.selectedRowKeysArr.length){
                        let deleteString = that.selectedRowKeysArr.join(',')
                        deleteArrServe({ids:deleteString}).then(res=>{
                            if(res.code == 200){
                                that.$message.success('删除成功')
                            }else{
                                that.$message.warning(res.message)
                            }              
                            that.renovateSearchTabel()
                        })
                    }else{
                        that.$message.warning('请选择删除项')
                    }
                },
                
            });
            
            
        },
        initiationYear(){//发起年度考评
            var that = this
                if(this.fyear){
                    if(that.selectedRowKeysArr.length){
                        let deleteString = that.selectedRowKeysArr.join(',')
                            batchInitHcyNdkp({ids:deleteString,fyear:this.fyear}).then(res=>{
                                if(res.code == 200){
                                    that.$message.success(res.message)
                                }else{
                                    that.$message.warning(res.message)
                                }              
                                that.renovateSearchTabel()
                            })
                        // this.$confirm({
                        //     title:'您确定要发起年度考评吗？',
                        //     // icon: createVNode(ExclamationCircleOutlined),
                        //     onOk() {
                                
                        //     },
                            
                        // });
                    }else{
                        that.$message.warning('请选择发起项')
                    }
                }else{
                    that.$message.warning('请输入年度')
                }
        },
        handleOk(val,id){
            if(val && !id){
                this.renovateSearchTabel()
            }else if(val && id){
                this.requestParams = {
                    ...this.requestParams,
                    renovate:true,
                }
            }
            
        },
        //编辑
        reviseClick(val){
            //console.log(val)
            var data = val[0]
            this.addEditParameter = {
                ...data,
                fuserid:data.fuserid,
                isEdit:true,
                fywflcode:blobType.fywflcodeArr[blobType.menuType.indexOf(this.$route.name.substring(this.$route.name.indexOf('__'),this.$route.name.indexOf('_a')+2))],
                sex:String(data.sex),
            }
            this.$refs.refModel.visibleShow = true
        },
        //删除
        deleteClick(val){
            var that = this
            this.$confirm({
                title:'您确认要删除吗？',
                // icon: createVNode(ExclamationCircleOutlined),
                onOk() {
                    deleteArrServe({ids:val[0].id}).then(res=>{
                        if(res.code == 200){
                            that.$message.success('删除成功')
                        }else{
                            that.$message.warning(res.message)
                        }              
                        that.renovateSearchTabel()
                    })
                },
                
            });
            
            
        },
        AddHandleOk(){
            this.renovateSearchTabel()
        }
    },

  }
</script>
<style scoped lang="less">
.observer-lbrary-pages{
    background: #fff;
}
.btn-box{
    display: flex;
    justify-content: space-between;
}
.blue-btn{
    border: 1px solid #5ca3e5 !important;
    background-color: #5ca3e5 !important;
    color: #fff !important;
}
.red-btn{
    border: 1px solid #ed5565 !important;
    background-color: #ed5565 !important;
}

</style>
<style lang="less">
.fyear-box{
    .ant-input-number{
        width: 105px !important;
    }
}
</style>